package com.welink.cmp.meatdata.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.convertor.RegionConvertor;
import com.welink.cmp.dao.metadata.dataobject.RegionDO;
import com.welink.cmp.dto.metadata.region.*;
import com.welink.cmp.meatdata.command.RegionCmdExe;
import com.welink.common.dto.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionAppService {

    @Autowired
    private RegionCmdExe regionCmdExe;

    /**
     * 添加区域
     *
     * @param addCmd 添加区域的命令对象
     * @return 新增区域的ID
     */
    public Long addRegion(RegionAddCmd addCmd) {
        return regionCmdExe.save(addCmd);
    }

    /**
     * 更新区域
     *
     * @param updCmd 更新区域的命令对象
     * @return 更新是否成功
     */
    public Boolean updateRegion(RegionUpdCmd updCmd) {
        return regionCmdExe.update(updCmd);
    }

    /**
     * 根据ID获取区域
     *
     * @param id 区域ID
     * @return 区域业务对象
     */
    public RegionCO getRegionById(Long id) {
        return regionCmdExe.queryById(id);
    }


    public List<RegionCO> getAllRegion(){
        final List<RegionDO> allRegion = regionCmdExe.getAllRegion();
        final List<RegionCO> coList = GenericConvertor.toCOList(allRegion, RegionCO.class);
        return coList;
    }

    /**
     * 分页查询区域
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括区域列表和分页信息
     */
    public PageResponse<RegionCO> regionPageQuery(RegionPageQry pageQry) {
        final Page<RegionDO> pageR = regionCmdExe.pageQuery(pageQry);
        List<RegionDO> records = pageR.getRecords();
        List<RegionCO> coList = RegionConvertor.toCOList(records);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }
}